<script setup>
import api from "./api.js";
import EditDialog from "./edit.vue";
import { ElMessage, ElMessageBox } from 'element-plus';  

const tableData = ref([]);
const paging = reactive({
  total: 1,
  pageNum: 1,
  pageSize: 10,
});
const tableRef = ref();
const editDialog = ref();

function handleAdd(parentId) {
  editDialog.value.open(null, parentId);
}

function handleUpdate(row) {
  editDialog.value.open(row);
}

// 删除方法
function handleDelete(row) {
  ElMessageBox.confirm('是否确认删除该路由?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    api.del(row.menuId).then(() => {
      getList();
      ElMessage.success('删除成功');
    }).catch(() => {
      ElMessage.error('删除失败');
    });
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
}

function getList() {
  // 获取列表数据
  api.list().then(response => {
    tableData.value = response.data;
  }).catch(error => {
    console.error(error);
  });
}

onMounted(() => {
  // 组件挂载时获取数据
  getList();
});
</script>

<template>
  <div class="organization-structure-main" style="height: 100%; display: flex; flex-direction: column;">

    <div style="margin-bottom: 15px;">
      <el-button @click="handleAdd">增加</el-button>
    </div>

    <vxe-table style="height: 100%; flex-grow: 1;" border="full" ref="tableRef"
      :tree-config="{ transform: true, rowField: 'menuId', parentField: 'parentId' }"
      :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" :data="tableData">
      <vxe-column field="menuName" title="名称" tree-node></vxe-column>
      <vxe-column field="orderNum" title="显示顺序"></vxe-column>
      <vxe-column field="path" title="路由地址"></vxe-column>
      <vxe-column width="250" title="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button size="small" link type="primary" @click="handleAdd(row.menuId)">增加子菜单</el-button>
          <el-button size="small" link type="primary" @click="handleUpdate(row)">修改</el-button>
          <el-button size="small" link type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </vxe-column>
    </vxe-table>

    <EditDialog ref="editDialog" @sub="getList"></EditDialog>

  </div>
</template>

<style scoped lang="scss">
.organization-structure-main {
  box-sizing: border-box;
  padding: 15px;
}
</style>
